package com.shoeshopping.service.pc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.shoeshopping.dao.BaseDao;
import com.shoeshopping.dao.joseph.UserDao;
import com.shoeshopping.dao.pc.GetIntoCartDao;
import com.shoeshopping.dao.pc.GetIntoOrderDao;
import com.shoeshopping.entity.DeliveryAddress;
import com.shoeshopping.entity.GoodsItem;
import com.shoeshopping.entity.Order;
import com.shoeshopping.entity.OrderItem;
import com.shoeshopping.entity.ShoppingCart;
import com.shoeshopping.entity.User;
import com.shoeshopping.utils.pc.Result;

@Transactional
@Service("getIntoOrderService")
public class GetIntoOrderServiceImpl implements GetIntoOrderService {

	@Resource(name="getIntoOrderDao")
	GetIntoOrderDao getIntoOrderDao;
	
	@Resource(name="baseDao")
	BaseDao baseDao;
	
	public String show() {
		
		Map Session = ActionContext.getContext().getSession();
		User user = (User) Session.get("user");
		String userid = user.getId();
		
		String hql = "from Order where user.id = ?";
		List param = new ArrayList();
		param.add(userid);
		List ordersList = baseDao.find(hql, param);
		if(ordersList!=null&&ordersList.size()>0){
			Order item = (Order)ordersList.get(0);
			OrderItem tmp = item.getOrderItem().get(0);
			tmp.getGoods().getName();
		}
		
		if(Session.get("ordersList") != null) {
			Session.remove("ordersList");
		}
		Session.put("ordersList", ordersList);
		
		return Result.SUCCESS;
	}
	
	public List save(OrderItem orderItem, DeliveryAddress da, String otherAdd) {
		
		System.out.println("+++++++++++++++++" + otherAdd);
		
		Map Session = ActionContext.getContext().getSession();
		User user = (User) Session.get("user");
		String userid = user.getId();
		
		String hql = "from User where id = ?";
		List param = new ArrayList();
		param.add(userid);
		User u = (User) baseDao.find(hql,param).get(0);
		List goodsItemList = u.getShoppingCart().getGoodsItems();
			
		Order order = new Order();
		List<OrderItem> orderItemList = new ArrayList<OrderItem>();
		for(Iterator it = goodsItemList.iterator(); it.hasNext();) {
			GoodsItem gi = (GoodsItem) it.next();
			OrderItem oi = new OrderItem();
			oi.setGoods(gi.getGoods());
			oi.setNum(gi.getNum());
			orderItemList.add(oi);
		}
		order.setOrderItem(orderItemList);
		order.setPayment(0);
		order.setUser(u);
		
		DeliveryAddress deliveryAddress = new DeliveryAddress();
		List<DeliveryAddress> dalist = u.getAddresses();
		if(dalist == null ) {
			dalist = new ArrayList<DeliveryAddress>();
		}
		if(otherAdd.equals("other")) {                     //新加入的地址
			deliveryAddress.setAddress(da.getAddress());
			deliveryAddress.setName(da.getName());
			deliveryAddress.setPhone(da.getPhone());	
			deliveryAddress.setUser(u);
			dalist.add(deliveryAddress);
			getIntoOrderDao.save(deliveryAddress);	
		} else {
			hql = "from DeliveryAddress where id = ?";
			param = new ArrayList();
			param.add(otherAdd);
			deliveryAddress = (DeliveryAddress) baseDao.find(hql, param).get(0);
			//deliveryAddress.setUser(u);
		}
		order.setDeliveryAddress(deliveryAddress);
		getIntoOrderDao.save(order);
		/*
		hql = "from Order where user.id = ?";
		param = new ArrayList();
		param.add(u.getId());*/
		
		List ordersList = new ArrayList();
		ordersList.add(order);
		
		if(Session.get("ordersList") != null) {
			Session.remove("ordersList");
		}
		Session.put("ordersList", ordersList);
		
		Session.remove("user");
		Session.put("user", u);
		Session.remove("goodsItemList");
		//Session.put("goodsItemList", list);
			
		List relist = new ArrayList();
		for(Iterator it = goodsItemList.iterator();it.hasNext();){
			GoodsItem g = (GoodsItem) it.next();
			relist.add(g);
		}
		
		return relist;
	}
	

}
